//
//  MainViewController.swift
//  新浪微博第一天
//
//  Created by 张志卓 on 15/11/3.
//  Copyright © 2015年 张志卓. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    
    @objc private func clickComposedButton(){
    
    
        print("点我了")
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

   
        addChildViewControllers()
        
        setupComposedButton()
        
    }
    
    
    override func viewWillAppear(animated: Bool) {
        
        super.viewWillAppear(animated)
        
        
        tabBar.bringSubviewToFront(composedButton)
    }
    
    
    //懒加载控件
    private lazy var composedButton:UIButton = UIButton(
    
        imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button"
        
    )

}


extension MainViewController{

    ///设置撰写按钮
    private func setupComposedButton(){
    
    
        tabBar.addSubview(composedButton)
        
        let count = childViewControllers.count
        
        let w = tabBar.bounds.width / CGFloat(count)-1
        
        composedButton.frame = CGRectInset(tabBar.bounds, w * 2, 0)
        
        composedButton.addTarget(self, action: "clickComposedButton", forControlEvents: .TouchUpInside)
    
    
    }
    
    
    
    ///添加所有控制器
    private func addChildViewControllers(){
        tabBar.tintColor = UIColor.orangeColor()
        
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
        
    
    }
    
    ///添加控制器
    private func addChildViewController(vc:UIViewController,title:String,imageName:String) {
     
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }

}
